package com.hongxun.design.sqlite.dao;

import android.content.Context;

import com.hongxun.design.sqlite.bean.OldBj;
import com.j256.ormlite.stmt.Where;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2018/5/3.
 */

public class OldBjDao extends BaseDao<OldBj> {

    public OldBjDao(Context context) {
        super(context);
    }

    /**
     * 返回项目导入的表计数量
     * @param proId
     * @return
     */
    public long getCount(String proId){
        long count=0;
        try {
            builder.setCountOf(true);
            count=dao.countOf(builder.where().eq("proId",proId).prepare());
            builder.setCountOf(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
    public List<OldBj> selectByLimit(String proId,int start,int limit){
        List<OldBj> list=new ArrayList<>();
        try {
            list=dao.queryBuilder().offset(Long.valueOf(start)).limit(Long.valueOf(limit)).where().eq("proId",proId).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<OldBj> selectByCode(String proId,String code){
        List<OldBj> list=new ArrayList<>();
        try {
            list=dao.queryBuilder()
                    .where().eq("proId",proId)
                    .and().eq("status","0")
                    .and().eq("bxCode",code)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<OldBj> selectByBjCode(String proId,String code){
        List<OldBj> list=new ArrayList<>();
        try {
            list=dao.queryBuilder().where().eq("proId",proId).and().eq("status","0").and().eq("bjCode",code).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<OldBj> selectByLike(String proId,String like){
        List<OldBj> list=new ArrayList<>();
        try {
            Where where=dao.queryBuilder().where();
            where.and(where.eq("proId",proId),where.eq("status","0"),where.or(where.like("name","%"+like+"%"),where.like("code","%"+like+"%")));
//            where.eq("proId",proId).and().eq("status","0").and().or(where.like("name","%"+like+"%"),where.like("code","%"+like+"%"));
//            list=dao.queryBuilder().where().eq("proId",proId).and().eq("status","0").and().like("name","%"+like+"%").or().like("code","%"+like+"%").query();
            list=where.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public void setStatus(String proId,String code,String bj_code){
        try {
            List<OldBj> list=dao.queryBuilder()
                    .where().eq("proId",proId)
                    .and().eq("code",code)
                    .and().eq("bjCode",bj_code)
                    .and().eq("status","1")
                    .query();
            if(list!=null && list.size()==1){
                OldBj o=list.get(0);
                o.setStatus("0");
                update(o);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
